//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Globalization;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Security
{
	partial class EditRoles : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		private int RoleID = -1;
		private void ActivateControls(bool enabled)
		{
			cboRoleGroups.Enabled = enabled;
			chkIsPublic.Enabled = enabled;
			chkAutoAssignment.Enabled = enabled;
			txtServiceFee.Enabled = enabled;
			txtBillingPeriod.Enabled = enabled;
			cboBillingFrequency.Enabled = enabled;
			txtTrialFee.Enabled = enabled;
			txtTrialPeriod.Enabled = enabled;
			cboTrialFrequency.Enabled = enabled;
			txtRSVPCode.Enabled = enabled;
		}
		private void BindGroups()
		{
			ArrayList arrGroups = RoleController.GetRoleGroups(PortalId);
			cboRoleGroups.Items.Add(new ListItem(Localization.GetString("GlobalRoles"), "-1"));
			foreach (RoleGroupInfo roleGroup in arrGroups) {
				cboRoleGroups.Items.Add(new ListItem(roleGroup.RoleGroupName, roleGroup.RoleGroupID.ToString()));
			}
		}
		private void UpdateFeeTextBoxes()
		{
			if (cboBillingFrequency.SelectedValue == "O") {
				txtBillingPeriod.Text = "1";
				txtBillingPeriod.Enabled = false;
			} else {
				txtBillingPeriod.Enabled = true;
			}
			if (cboTrialFrequency.SelectedValue == "O") {
				txtTrialPeriod.Text = "1";
				txtTrialPeriod.Enabled = false;
			} else {
				txtTrialPeriod.Enabled = true;
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cboBillingFrequency.SelectedIndexChanged += new EventHandler(cboBillingFrequency_SelectedIndexChanged);
			cboTrialFrequency.SelectedIndexChanged += new EventHandler(cboTrialFrequency_SelectedIndexChanged);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdManage.Click += new EventHandler(cmdManage_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);
			txtRSVPCode.TextChanged += new EventHandler(txtRSVPCode_TextChanged);

			try {
				if ((Request.QueryString["RoleID"] != null)) {
					RoleID = Int32.Parse(Request.QueryString["RoleID"]);
				}
				PortalController objPortalController = new PortalController();
				PortalInfo objPortalInfo = objPortalController.GetPortal(PortalSettings.PortalId);
				if ((objPortalInfo == null || string.IsNullOrEmpty(objPortalInfo.ProcessorUserId))) {
					lblProcessorWarning.Visible = true;
				} else {
					trBillingPeriod.Visible = true;
					trServiceFee.Visible = true;
					trTrialFee.Visible = true;
					trTrialPeriod.Visible = true;
				}
				if (Page.IsPostBack == false) {
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
					RoleController objUser = new RoleController();
					Common.Lists.ListController ctlList = new Common.Lists.ListController();
					Common.Lists.ListEntryInfoCollection colFrequencies = ctlList.GetListEntryInfoCollection("Frequency", "");
					cboBillingFrequency.DataSource = colFrequencies;
					cboBillingFrequency.DataBind();
					cboBillingFrequency.Items.FindByValue("N").Selected = true;
					cboTrialFrequency.DataSource = colFrequencies;
					cboTrialFrequency.DataBind();
					cboTrialFrequency.Items.FindByValue("N").Selected = true;
					BindGroups();
					ctlIcon.FileFilter = Common.Globals.glbImageFileTypes;
					if (RoleID != -1) {
						lblRoleName.Visible = true;
						txtRoleName.Visible = false;
						valRoleName.Enabled = false;
						RoleInfo objRoleInfo = objUser.GetRole(RoleID, PortalSettings.PortalId);
						if (objRoleInfo != null) {
							lblRoleName.Text = objRoleInfo.RoleName;
							txtDescription.Text = objRoleInfo.Description;
							if (cboRoleGroups.Items.FindByValue(objRoleInfo.RoleGroupID.ToString()) != null) {
								cboRoleGroups.ClearSelection();
								cboRoleGroups.Items.FindByValue(objRoleInfo.RoleGroupID.ToString()).Selected = true;
							}
							if (objRoleInfo.BillingFrequency != "N") {
								txtServiceFee.Text = objRoleInfo.ServiceFee.ToString("N2", CultureInfo.CurrentCulture);
								txtBillingPeriod.Text = objRoleInfo.BillingPeriod.ToString();
								if (cboBillingFrequency.Items.FindByValue(objRoleInfo.BillingFrequency) != null) {
									cboBillingFrequency.ClearSelection();
									cboBillingFrequency.Items.FindByValue(objRoleInfo.BillingFrequency).Selected = true;
								}
							}
							if (objRoleInfo.TrialFrequency != "N") {
								txtTrialFee.Text = objRoleInfo.TrialFee.ToString("N2", CultureInfo.CurrentCulture);
								txtTrialPeriod.Text = objRoleInfo.TrialPeriod.ToString();
								if (cboTrialFrequency.Items.FindByValue(objRoleInfo.TrialFrequency) != null) {
									cboTrialFrequency.ClearSelection();
									cboTrialFrequency.Items.FindByValue(objRoleInfo.TrialFrequency).Selected = true;
								}
							}
							chkIsPublic.Checked = objRoleInfo.IsPublic;
							chkAutoAssignment.Checked = objRoleInfo.AutoAssignment;
							txtRSVPCode.Text = objRoleInfo.RSVPCode;
							if (!String.IsNullOrEmpty(txtRSVPCode.Text)) {
								lblRSVPLink.Text = Common.Globals.AddHTTP(Common.Globals.GetDomainName(Request)) + "/" + Common.Globals.glbDefaultPage + "?rsvp=" + txtRSVPCode.Text + "&portalid=" + PortalId.ToString();
							}
							ctlIcon.Url = objRoleInfo.IconFile;
							UpdateFeeTextBoxes();
						} else {
							Response.Redirect(Common.Globals.NavigateURL("Security Roles"));
						}
						if (RoleID == PortalSettings.AdministratorRoleId || RoleID == PortalSettings.RegisteredRoleId) {
							cmdDelete.Visible = false;
							ActivateControls(false);
						}
						if (RoleID == PortalSettings.RegisteredRoleId) {
							cmdManage.Visible = false;
						}
					} else {
						cmdDelete.Visible = false;
						cmdManage.Visible = false;
						lblRoleName.Visible = false;
						txtRoleName.Visible = true;
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void cboBillingFrequency_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateFeeTextBoxes();
		}
		protected void cboTrialFrequency_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateFeeTextBoxes();
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try {
				if (Page.IsValid) {
					float sglServiceFee = 0;
					int intBillingPeriod = 1;
					string strBillingFrequency = "N";
					if (cboBillingFrequency.SelectedItem.Value == "N" && !String.IsNullOrEmpty(txtServiceFee.Text)) {
						DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("IncompatibleFee", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						return;
					}
					if (!String.IsNullOrEmpty(txtServiceFee.Text) && !String.IsNullOrEmpty(txtBillingPeriod.Text) && cboBillingFrequency.SelectedItem.Value != "N") {
						sglServiceFee = float.Parse(txtServiceFee.Text);
						intBillingPeriod = int.Parse(txtBillingPeriod.Text);
						strBillingFrequency = cboBillingFrequency.SelectedItem.Value;
					}
					float sglTrialFee = 0;
					int intTrialPeriod = 1;
					string strTrialFrequency = "N";
					if (sglServiceFee != 0 && !String.IsNullOrEmpty(txtTrialFee.Text) && !String.IsNullOrEmpty(txtTrialPeriod.Text) && cboTrialFrequency.SelectedItem.Value != "N") {
						sglTrialFee = float.Parse(txtTrialFee.Text);
						intTrialPeriod = int.Parse(txtTrialPeriod.Text);
						strTrialFrequency = cboTrialFrequency.SelectedItem.Value;
					}
					RoleController objRoleController = new RoleController();
					RoleInfo objRoleInfo = new RoleInfo();
					objRoleInfo.PortalID = PortalId;
					objRoleInfo.RoleID = RoleID;
					objRoleInfo.RoleGroupID = int.Parse(cboRoleGroups.SelectedValue);
					objRoleInfo.RoleName = txtRoleName.Text;
					objRoleInfo.Description = txtDescription.Text;
					objRoleInfo.ServiceFee = sglServiceFee;
					objRoleInfo.BillingPeriod = intBillingPeriod;
					objRoleInfo.BillingFrequency = strBillingFrequency;
					objRoleInfo.TrialFee = sglTrialFee;
					objRoleInfo.TrialPeriod = intTrialPeriod;
					objRoleInfo.TrialFrequency = strTrialFrequency;
					objRoleInfo.IsPublic = chkIsPublic.Checked;
					objRoleInfo.AutoAssignment = chkAutoAssignment.Checked;
					objRoleInfo.RSVPCode = txtRSVPCode.Text;
					objRoleInfo.IconFile = ctlIcon.Url;
					if (RoleID == -1) {
						if (objRoleController.GetRoleByName(PortalId, objRoleInfo.RoleName) == null) {
							objRoleController.AddRole(objRoleInfo);
						} else {
							DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("DuplicateRole", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
							return;
						}
					} else {
						objRoleController.UpdateRole(objRoleInfo);
					}
					DataCache.RemoveCache("GetRoles");
					Response.Redirect(Common.Globals.NavigateURL());
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, System.EventArgs e)
		{
			try {
				RoleController objUser = new RoleController();
				objUser.DeleteRole(RoleID, PortalSettings.PortalId);
				DataCache.RemoveCache("GetRoles");
				Response.Redirect(Common.Globals.NavigateURL());
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			try {
				Response.Redirect(Common.Globals.NavigateURL());
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdManage_Click(System.Object sender, System.EventArgs e)
		{
			try {
				Response.Redirect(this.EditUrl("RoleId", RoleID.ToString(), "User Roles"));
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void txtRSVPCode_TextChanged(object sender, System.EventArgs e)
		{
			lblRSVPLink.Text = Common.Globals.AddHTTP(Common.Globals.GetDomainName(Request)) + "/" + Common.Globals.glbDefaultPage + "?rsvp=" + txtRSVPCode.Text + "&portalid=" + PortalId.ToString();
		}
	}
}
